from time import sleep

from selenium import  webdriver
from HTMLTestRunner import HTMLTestRunner
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import unittest


#打开登录界面
class JXC_Login_Test(unittest.TestCase):
    def setUp(self):
        driver = webdriver.Firefox()
        driver.get('http://localhost/jxc/index.asp')
    def tearDown(self):
        self.driver.quit()
#成功测试
    def test_login_true(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        #断言
        self.assertIn('系统登陆', self.driver.page_source)
#测试删测单位
    def test_JXC_Del(self):
        sleep(2)
        self.driver.switch_to.frame('left')
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        sleep(2)
        danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(danwei_css).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(2)
        cha_css = '.toptable > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(3) > input:nth-child(1)'
        self.driver.find_element_by_css_selector(cha_css).click()
        del_css = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(del_css).click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(2)
        #断言
        self.assertNotIn('name="ID"', self.driver.page_source)
#测试添加单位
    def test_JXC_ADDdanwei(self):
        sleep(2)
        add_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(add_css).click()
        sleep(2)
        self.driver.find_element_by_name('danwei').send_keys('吨')
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        #断言
        self.assertIn('吨', self.driver.page_source)
#测试添加大类
    def test_JXC_ADDdalei(self):
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        sleep(2)
        # 点击菜单，系统设置-产品大类管理
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        dalei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(dalei_css).click()
        # 从当前框架切换到菜单框架中
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(2)
        # 点击添加大类
        addd_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(addd_css).click()
        sleep(2)
        self.driver.find_element_by_name('bigclass').send_keys('产品')
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        self.assertIn('产品', self.driver.page_source)
#测试添加小类
    def test_JXC_ADDxiaolei(self):
        # 从当前框架切换到菜单框架中
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        sleep(2)

        # 点击菜单，系统设置-产品小类管理
        sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(sysset_css).click()
        dalei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
        self.driver.find_element_by_css_selector(dalei_css).click()

        # 从当前框架切换到菜单框架中
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(2)

        # 点击添加小类
        addx_css = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(addx_css).click()
        sleep(2)
        self.driver.find_element_by_name('smallclass').send_keys('产品')
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        self.assertIn('产品', self.driver.page_source)
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(JXC_Login_Test('test_login_true'))
    suite.addTest(JXC_Login_Test('test_JXC_ADDdanwei'))
    suite.addTest(JXC_Login_Test('test_JXC_ADDdalei'))
    suite.addTest(JXC_Login_Test('test_JXC_ADDxiaolei'))
    return suite


with open('D:\ht.html','wb')as f:
    runner = HTMLTestRunner(stream=f, title='进存销测试报告', description='报告人：计171 苑博 _20200622')
    runner.run(test_suite())